package com.wing.cms.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.common.CommonSpringJDBC;
import com.wing.common.util.PageBean;
import com.wing.cms.domain.Article;

@Repository
public class ArticleDao extends CommonSpringJDBC<Article, String> {

	public PageBean findPageList(int currentPage, int pageSize,String siteId, String grpid,String s) {
		String where= " a.site_id='"+siteId+"' and a.id=ag.article_id and ag.artgroup_id='"+grpid+"'";
		String sql1=" SELECT a.* FROM cms_article a,cms_article_group ag where "+where+" ORDER BY a.ordernum asc, a.create_Time desc " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(a.id) from  cms_article a,cms_article_group ag  where"+where;
		if(s!=null){
			where= " a.site_id='"+siteId+"' and a.art_name like '%"+s+"%'";
			sql1=" SELECT a.* FROM cms_article a where "+where+" ORDER BY a.ordernum asc,a.create_Time desc " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
			sql2 = "select count(a.id) from  cms_article a  where"+where;
		}
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}
	
	public PageBean findByGroupId(int currentPage, int pageSize, String groupId,String seachv) {
		String where= " a.id = b.article_id and b.artgroup_id = '"+groupId+"' ";
		if(seachv!=null && !"".equals(seachv)){
			where = " a.id = b.article_id and b.artgroup_id = '"+groupId+"'  and a.art_name like '%"+seachv+"%'  ";
		}
		String sql1=" select a.* from cms_article a ,cms_article_group b where "+where+" order by  b.istop ,b.toptime desc ,a.ordernum asc,a.create_Time desc " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(a.id) from  cms_article a ,cms_article_group b  where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}
	
	public List<Map<String, Object>> findBySiteId(String siteId) {
		String sql=" select a.* from cms_article a  where a.site_id = ? order by a.ordernum asc,a.create_time";
		Object[] objs = { siteId };
		return super.getSimpleJdbcTemplate().queryForList(sql, objs);
	}
}
